<?php
declare(strict_types=1);
/**
 * 分销用户，小店
 * Date: 2020年2月26日
 * Time: 下午14:29:11
 */
namespace App\Model\BbcModel;

use Service\Code\Normal;
use App\Model\BaseModel\BaseBbcModel;

class Mnuser extends BaseBbcModel {

    const TYPE_STORE_QZ = 3;//前置仓店

    public $table = 'ly_mn_user';

    /**
     * 判断店铺是否存在或是否是关联店铺
     */
    public static function validate($param) {
        if (isset($param['mnid']) && $param['mnid'] == 2) {
            //小B小程序
            if (empty($param['mid'])) {
                return Normal::returnCode(Normal::FAIL, '请先登录');
            }
            $store = self::where('mid', $param['mid'])->where('status', 1)->where('check_status', 1)->where('new_store', 1)->where('is_pay', 1)->where('pid', $param['cid'])->first();
            if (empty($store)) {
                return Normal::returnCode(Normal::FAIL, '请先开通小店');
            }
            $store = $store->toArray();
            if (isset($param['mn_sid']) && $param['mn_sid'] > 0) {
                if ($param['mn_sid'] == $store['id']) {
                    return Normal::returnCode(Normal::SUC, 'ok', $store);
                }
                //判断是否是关联店铺
                $checkRelation = ViceStore::checkRelation(['main_mn_sid' => $store['id'], 'vice_mn_sid' => $param['mn_sid'], 'cid' => $param['cid']]);
                if ($checkRelation) {
                    $info = self::where('id', $param['mn_sid'])->where('status', 1)->where('check_status', 1)->where('new_store', 1)->where('is_pay', 1)->where('pid', $param['cid'])->first();
                    if (empty($info)) {
                        return Normal::returnCode(Normal::FAIL, '关联的小店不存在');
                    }
                    return Normal::returnCode(Normal::SUC, 'ok', $info->toArray());
                }
                return Normal::returnCode(Normal::FAIL, '该店铺不是你的关联店铺');
            }
            return Normal::returnCode(Normal::SUC, 'ok', $store);
        }
        //C端小程序
        if (empty($param['mn_sid'])) {
            return Normal::returnCode(Normal::FAIL, '小店不存在');
        }
        $info = self::where('id', $param['mn_sid'])->where('status', 1)->where('check_status', 1)->where('new_store', 1)->where('is_pay', 1)->where('pid', $param['cid'])->first();
        if (empty($info)) {
            return Normal::returnCode(Normal::FAIL, '小店不存在');
        }
        return Normal::returnCode(Normal::SUC, 'ok', $info->toArray());
    }

}
